package com.example.alpha.campusbase.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.avos.avoscloud.im.v2.AVIMClient;
import com.avos.avoscloud.im.v2.AVIMConversation;
import com.avos.avoscloud.im.v2.AVIMException;
import com.avos.avoscloud.im.v2.callback.AVIMClientCallback;
import com.avos.avoscloud.im.v2.callback.AVIMConversationCallback;
import com.avos.avoscloud.im.v2.callback.AVIMConversationCreatedCallback;
import com.avos.avoscloud.im.v2.messages.AVIMTextMessage;
import com.dexafree.materialList.card.Card;
import com.dexafree.materialList.card.CardProvider;
import com.dexafree.materialList.view.MaterialListView;
import com.example.alpha.campusbase.R;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.squareup.picasso.RequestCreator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 聊天界面碎片
 */
public class ChatFragment extends HandlerFragment {

    private static final int MESSAGE_LEFT=0;
    private static final int MESSAGE_RIGHT=1;
    private Context mContext;
    private MaterialListView mChatListView;
    private MaterialEditText mSendText;

    

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mChatView=inflater.inflate(R.layout.chat_layout,container,false);
        mChatListView= (MaterialListView) mChatView.findViewById(R.id.chat_message_list);
        mContext=getContext();

        mSendText= (MaterialEditText) mChatView.findViewById(R.id.chat_message_edit);
        Button mSendButton= (Button) mChatView.findViewById(R.id.chat_message_send);
        mSendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMessage(v,mSendText.getText().toString());
            }
        });
        fillArray();
        return mChatView;
    }

    public void fillArray(){
        List<Card> cards=new ArrayList<>();
        for (int i=0;i<2;i++){
            cards.add(getCard(i,null));
        }
        mChatListView.getAdapter().addAll(cards);
    }

    private Card getCard(final int position,String message){


        final CardProvider provider = new Card.Builder(mContext)
                .withProvider(new CardProvider<>())
                .setDrawableConfiguration(new CardProvider.OnImageConfigListener() {
                    @Override
                    public void onImageConfigure(@NonNull RequestCreator requestCreator) {
                        requestCreator.fit();
                    }
                });
        // TODO: 2016/6/10 改为具体的动态更新列表方法
        switch (position%2){
            case 0:
                provider.setTitleColor(getResources().getColor(R.color.white));
                provider.setLayout(R.layout.chat_message_left);
                provider.setDrawable(R.drawable.m01);
                provider.setTitle("老师今天的讲课课件你有保存么？");
                provider.setTitleGravity(Gravity.CENTER_VERTICAL);
                break;
            case 1:
                provider.setTitleColor(getResources().getColor(R.color.black_text));
                provider.setLayout(R.layout.chat_meaage_right);
                provider.setDrawable(R.drawable.m02);
                provider.setTitle("有啊，我晚上邮件发给你。");
                provider.setTitleGravity(Gravity.CENTER_VERTICAL);
                break;
        }

        return provider.endConfig().build();
    }

    @Override
    protected Boolean onBackPressed() {
        return true;
    }

    private void sendMessage(View view, final String message){
        if (!TextUtils.isEmpty(message)){
            //添加信息到视图
            Log.d("-----","1");
            changeMessageList(message,MESSAGE_RIGHT);
            Log.d("-----","2");
            AVIMClient tom = AVIMClient.getInstance("gaoyanqiang");
            Log.d("-----","3");
            // 与服务器连接
            tom.open(new AVIMClientCallback() {

                @Override
                public void done(AVIMClient client, AVIMException e) {
                    Log.d("-----","4");
                    Log.d("dskndijsbdhsb",e.toString());
                    if (e == null) {
                        // 创建与Jerry之间的对话
                        Log.d("-----","5");
                        client.createConversation(Arrays.asList("Jerry"), "Tom & Jerry", null,
                                new AVIMConversationCreatedCallback() {

                                    @Override
                                    public void done(AVIMConversation conversation, AVIMException e) {
                                        if (e == null) {
                                            AVIMTextMessage msg = new AVIMTextMessage();
                                            msg.setText(message);
                                            // 发送消息
                                            conversation.sendMessage(msg, new AVIMConversationCallback() {

                                                @Override
                                                public void done(AVIMException e) {
                                                    if (e == null) {
                                                        Log.d("Tom & Jerry", "发送成功！");
                                                    }
                                                }
                                            });
                                        }
                                    }
                                });
                    }
                }
            });
        }
    }

    //更新视图
    public void changeMessageList(String message,int loc){
        mChatListView.getAdapter().add(getCard(MESSAGE_RIGHT,message));
        mSendText.setText("");
    }
}
